<?php
	namespace Qzn\View;
	
	require_once('./Model/message.php');
	require_once('./Model/comment.php');
	
	class WallView {
		private $m_post = "Post";
		private $m_comment = "Comment";
		private $m_mlike = "Like Post";
		private $m_mdislike = "Dislike Post";
		private $m_clike = "Like Comment";
		private $m_cdislike = "Dislike Comment";
		private $m_postText = "postText";
		private $m_commentText = "commentText";
		
		/**
		 * @return string $html
		 */
		public function DoWallBox(\Qzn\Model\WallHandler $wh, \Qzn\View\LoginView $lw, \Qzn\Model\ProfileHandler $ph, \Qzn\Model\Validate $v) {
			$html = "<div>
						<h2>Your Wall!</h2>
						<form method='post'>
							<textarea name='$this->m_postText' placeholder='Post goes here!'></textarea>
							<input type='submit' name='$this->m_post' value='Post something!'/>
						</form>";
			
			$messages = $wh->GetMessages($lw);
			$messageLikes;
			$messageLikesRet;
			$likes = 0;
			$message;
			
			foreach($messages as $a) {
				foreach($a as $b) {
					$messageLikesRet = null;
					$likes = 0;
					$messageLikes = $wh->GetMessageLikes($b[0]);
					
					foreach($messageLikes as $c) {
						$likes += $c[1] === 0 ? -1 : 1;
						$messageLikesRet[$c[0]] = $c[1];
					}
					
					$message = new \Qzn\Model\Message($b[0], $b[1], $b[2], $b[3], $b[4], $likes, $messageLikesRet);
					$html .= $this->DoMessageBox($message, $wh, $lw, $ph, $v);
				}
			}
			
			$html .= "</div>";
			
			return $html;
		}
		
		/**
		 * @return string $html
		 * @param Message $message
		 * @param WallHandler $wh
		 * @param LoginView $lw
		 * @param ProfileHandler $ph
		 * @param Validate $v
		 */
		public function DoMessageBox(\Qzn\Model\Message $message, \Qzn\Model\WallHandler $wh, \Qzn\View\LoginView $lw, \Qzn\Model\ProfileHandler $ph, \Qzn\Model\Validate $v) {
			$pp = $ph->GetPicturePath($message->m_userId);
			$html = "<div>
						<h4><a href='?profile=$message->m_userId'>$message->m_user</a> <a href='?message=$message->m_messageId'>$message->m_date</a></h4>
						<img src='$pp' alt='Profile Picture' />
						<p>$message->m_message</p>";
			
			if ($message->m_messageLikesArray == null ? true : !array_key_exists($lw->GetUserPk(), $message->m_messageLikesArray)) {
				$html .= "<form method='post'>
							<input type='submit' name='$message->m_messageId' value='$this->m_mlike'/>
							<input type='submit' name='$message->m_messageId' value='$this->m_mdislike'/>
						</form>";
			} else {
				$dislike = $message->m_messageLikesArray[$lw->GetUserPk()] === 0 ? true : false;
				if ($dislike) {
					$html .= "<form method='post'>
							<input type='submit' name='$message->m_messageId' value='$this->m_mlike'/>
						</form>";
				} else {
					$html .= "<form method='post'>
							<input type='submit' name='$message->m_messageId' value='$this->m_mdislike'/>
						</form>";
				}
			}
			
			if ($message->m_messageLikes === 1) {
				$html .= "<h4>$message->m_messageLikes Like</h4>";
			} else if ($message->m_messageLikes >= 0) {
				$html .= "<h4>$message->m_messageLikes Likes</h4>";
			} else if ($message->m_messageLikes === -1) {
				$html .= "<h4>$message->m_messageLikes Dislike</h4>";
			} else {
				$html .= "<h4>$message->m_messageLikes Dislikes</h4>";
			}
			
			$comments = $wh->GetComments($message->m_messageId);
			$commentLikes;
			$commentLikesRet;
			$likes = 0;
			$comment;
			
			foreach($comments as $a) {
				$commentLikesRet = null;
				$likes = 0;
				$commentLikes = $wh->GetCommentLikes($a[0]);
				
				foreach($commentLikes as $b) {
					$likes += $b[1] === 0 ? -1 : 1;
					$commentLikesRet[$b[0]] = $b[1];
				}
				
				$comment = new \Qzn\Model\Comment($a[0], $a[1], $a[2], $a[3], $a[4], $likes, $commentLikesRet);
				$html .= $this->DoCommentBox($comment, $lw, $ph, $v);
			}
			
			$html .= "<form method='post'>
							<textarea name='$this->m_commentText' placeholder='Comment goes here!'></textarea>
							<input type='submit' name='$message->m_messageId' value='$this->m_comment'/>
						</form>
					</div>";
			return $html;
		}
		
		/**
		 * @return string $html
		 * @param Comment $comment
		 * @param LoginView $lw
		 * @param ProfileHandler $ph
		 * @param Validate $v
		 */
		public function DoCommentBox(\Qzn\Model\Comment $comment, \Qzn\View\LoginView $lw, \Qzn\Model\ProfileHandler $ph, \Qzn\Model\Validate $v) {
			$pp = $ph->GetPicturePath($comment->m_userId);
			$html = "<div style='padding-left:50px'>
						<h4><a href='?profile=$comment->m_userId'>$comment->m_user</a> $comment->m_date</h4>
						<img src='$pp' alt='Profile Picture' />
						<p>$comment->m_comment</p>";
			
			if ($comment->m_commentLikesArray == null ? true : !array_key_exists($lw->GetUserPk(), $comment->m_commentLikesArray)) {
				$html .= "<form method='post'>
							<input type='submit' name='$comment->m_commentId' value='$this->m_clike'/>
							<input type='submit' name='$comment->m_commentId' value='$this->m_cdislike'/>
						</form>";
			} else {
				$dislike = $comment->m_commentLikesArray[$lw->GetUserPk()] === 0 ? true : false;
				
				if ($dislike) {
					$html .= "<form method='post'>
							<input type='submit' name='$comment->m_commentId' value='$this->m_clike'/>
						</form>";
				} else {
					$html .= "<form method='post'>
							<input type='submit' name='$comment->m_commentId' value='$this->m_cdislike'/>
						</form>";
				}
			}
				
			if ($comment->m_commentLikes === 1) {
				$html .= "<h4>$comment->m_commentLikes Like</h4>
						</div>";
			} else if ($comment->m_commentLikes >= 0) {
				$html .= "<h4>$comment->m_commentLikes Likes</h4>
						</div>";
			} else if ($comment->m_commentLikes === -1) {
				$html .= "<h4>$comment->m_commentLikes Dislike</h4>
						</div>";
			} else {
				$html .= "<h4>$comment->m_commentLikes Dislikes</h4>
						</div>";
			}
			
			return $html;
		}
		
		/**
		 * @return int
		 */
		public function GetMessagePk() {
			if (count(array_keys($_POST, "$this->m_comment")) == 1) {
				$ret = array_keys($_POST, "$this->m_comment");
				$ret = end($ret);
				return $ret;
			}
			return null;
		}
		
		/**
		 * @return string $message
		 */
		public function GetMessage() {
			if(isset($_POST[$this->m_postText])) {
				return $_POST[$this->m_postText];
			}
			return null;
		}
		
		/**
		 * @return string $comment
		 */
		public function GetComment() {
			if(isset($_POST[$this->m_commentText])) {
				return $_POST[$this->m_commentText];
			}
			return NULL;
		}
		
		/**
		 * @return int
		 */
		public function GetLikeCommentPk() {
			if (count(array_keys($_POST, "$this->m_clike")) == 1) {
				$ret = array_keys($_POST, "$this->m_clike");
				$ret = end($ret);
				return $ret;
			} else if (count(array_keys($_POST, "$this->m_cdislike")) == 1) {
				$ret = array_keys($_POST, "$this->m_cdislike");
				$ret = end($ret);
				return $ret;
			} 
			return null;
		}

		/**
		 * @return int
		 */
		public function GetLikeMessagePk() {
			if (count(array_keys($_POST, "$this->m_mlike")) == 1) {
				$ret = array_keys($_POST, "$this->m_mlike");
				$ret = end($ret);
				return $ret;
			} else if (count(array_keys($_POST, "$this->m_mdislike")) == 1) {
				$ret = array_keys($_POST, "$this->m_mdislike");
				$ret = end($ret);
				return $ret;
			} 
			return null;
		}

		/**
		 * @return boolean
		 */
		public function TriedToPost() {
			if (isset($_POST[$this->m_post])) {
				return true;
			}
			
			return false;
		}
		
		/**
		 * @return boolean
		 */
		public function TriedToMessageLike() {
			if (count(array_keys($_POST, $this->m_mlike)) == 1) {
				return true;
			}
			
			return false;
		}
		
		/**
		 * @return boolean
		 */
		public function TriedToCommentLike() {
			if (count(array_keys($_POST, $this->m_clike)) == 1) {
				return true;
			}
			
			return false;
		}
		
		/**
		 * @return boolean
		 */
		public function TriedToMessageDislike() {
			if (count(array_keys($_POST, $this->m_mdislike)) == 1) {
				return true;
			}
			
			return false;
		}
		 
		 /**
		 * @return boolean
		 */
		public function TriedToCommentDislike() {
			if (count(array_keys($_POST, $this->m_cdislike)) == 1) {
				return true;
			}
			
			return false;
		}
		
		/**
		 * @return boolean
		 */
		public function TriedToComment() {
			if (count(array_keys($_POST, "$this->m_comment")) == 1) {
				return true;
			}
			
			return false;
		}
	}
?>